function withData(param) {
  return param < 10 ? '0' + param : '' + param;
}
function getSlotTime() {
  return [
    '00:00 ~ 03:00',
    '03:00 ~ 06:00',
    '06:00 ~ 08:00',
    '08:00 ~ 09:00',
    '09:00 ~ 10:00',
    '10:00 ~ 11:00',
    '11:00 ~ 12:00',
    '12:00 ~ 13:00',
    '13:00 ~ 14:00',
    '14:00 ~ 15:00',
    '15:00 ~ 16:00',
    '16:00 ~ 17:00',
    '16:00 ~ 17:00',
    '17:00 ~ 18:00',
    '18:00 ~ 21:00',
    '21:00 ~ 24:00'
  ];
}
function getLoopArray(start, end) {
  var start = start || 0;
  var end = end || 1;
  var array = [];
  for (var i = start; i <= end; i++) {
    array.push(withData(i));
  }
  return array;
}
function getMonthDay(year, month) {
  var flag = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0),
    array = null;

  switch (month) {
    case '01':
    case '03':
    case '05':
    case '07':
    case '08':
    case '10':
    case '12':
      array = getLoopArray(1, 31);
      break;
    case '04':
    case '06':
    case '09':
    case '11':
      array = getLoopArray(1, 30);
      break;
    case '02':
      array = flag ? getLoopArray(1, 29) : getLoopArray(1, 28);
      break;
    default:
      array = '月份格式不正确，请重新输入！';
  }
  return array;
}
function getNewDateArry() {
  // 当前时间的处理
  var newDate = new Date();
  var year = withData(newDate.getFullYear()),
    mont = withData(newDate.getMonth() + 1),
    date = withData(newDate.getDate()),
    hour = withData(newDate.getHours()),
    minu = withData(newDate.getMinutes()),
    seco = withData(newDate.getSeconds());

  return [year, mont, date, hour, minu, seco];
}
function dateTimePicker(timeType, currentIndex) {
  const startYear = new Date().getFullYear();
  const endYear = startYear + 5;
  const defaultDate = getNewDateArry();
  const startMonth = defaultDate[1];
  const startDate = defaultDate[2];
  const startHour = defaultDate[3];
  const startMinute = defaultDate[4];
  const dateTime = [];
  const dateTimeArray = [[], []];
  dateTimeArray[0] = getLoopArray(startYear, endYear);
  dateTimeArray[1] = getLoopArray(1, 12);
  dateTimeArray.forEach((current, index) => {
    dateTime.push(current.indexOf(defaultDate[index]));
  });

  const result = dateTimeArray[0].map(item => {
    return [
      item,
      dateTimeArray[1]
        .filter(month => {
          if (+item === startYear) {
            return month >= startMonth;
          }
          return true;
        })
        .map(i => {
          return [
            i,
            getMonthDay(item, i).filter(day => {
              if (+item == startYear && +i === +startMonth) {
                return +day >= +startDate;
              }
              return true;
            })
          ];
        })
    ];
  });

  const array = [];

  result.forEach(year => {
    const arr = [];
    year[1].forEach(month => {
      month[1].forEach(day => {
        arr.push([year[0], month[0], day]);
      });
    });

    array.push(arr);
  });

  const a = array.flat().map(item => {
    return item.join('-');
  });

  let endResult = [a, getLoopArray(0, 23).filter(item => {
    const [year, month, day] = a[currentIndex || 0].split('-');
    if (+year === +startYear && +month === +startMonth && +day === +startDate) {
      return startHour <= item;
    }
    return true;
  }), getLoopArray(0, 59).filter(item => {
    const [year, month, day] = a[currentIndex || 0].split('-');
    if (+year === +startYear && +month === +startMonth && +day === +startDate) {
      return startMinute <= item;
    }
    return true;
  })];

  if (timeType === 'slot') {
    endResult = [
      a,
      getSlotTime().filter(item => {
        const [year, month, day] = a[currentIndex || 0].split('-');
        const hour_0 = item.split(' ~ ')[0].split(':')[0];
        if (+year === +startYear && +month === +startMonth && +day === +startDate) {
          return startHour <= hour_0;
        }
        return true;
      })
    ];
  }
  return {
    dateTimeArray: endResult,
    dateTime: dateTime
  };
}

module.exports = {
  dateTimePicker: dateTimePicker,
  getMonthDay: getMonthDay,
};